﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlanningGIS.Framework.Config;
using PlanningGIS.Win.Controls;
using PlanningGIS.Oracle;
using PlanningGIS.Util.Net;
using System.IO;
using System.Data;
using PlanningGIS.Framework;
using Oracle.DataAccess.Client;

namespace PlanningGIS.Raster
{
    /// <summary>
    /// 栅格数据操作基础类
    /// </summary>
    public static class RasterHelper
    {
        /// <summary>
        /// FTP数据类型
        /// </summary>
        public const string FTP_TYPE = "RASTER";

        /// <summary>
        /// 栅格配置
        /// </summary>
        private static RasterConfig _RasterConfig = null;
        /// <summary>
        /// 栅格配置
        /// </summary>
        public static RasterConfig RasterConfig
        {
            get
            {
                if (_RasterConfig == null)
                {
                    try
                    {
                        _RasterConfig = ConfigHelper.GetObject<RasterConfig>();
                    }
                    catch (Exception ex)
                    {
                        MsgHelper.ShowErrorMsgbox("配置序列化出错：" + ex.Message);
                    }
                }
                return _RasterConfig;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private static DbConfig _DbConfig = null;
        /// <summary>
        /// 栅格数据库配置
        /// </summary>
        public static DbConfig DbConfig
        {
            get
            {
                if (_DbConfig == null)
                {
                    _DbConfig = new DbConfig(RasterConfig.ConnectName);
                }
                return _DbConfig;
            }
        }

        /// <summary>
        /// Ftp操作对象
        /// </summary>
        private static FTPRequest _ftp = null;

        /// <summary>
        /// 获取Ftp操作对象
        /// </summary>
        public static FTPRequest Ftp
        {
            get
            {
                if (_ftp == null)
                {
                    string uri = string.Format("ftp://{0}/", AppConfig.SystemConfig.FTP.Server);
                    if (AppConfig.SystemConfig.FTP.Port != 21)
                        uri = string.Format("ftp://{0}:{1}/", AppConfig.SystemConfig.FTP.Server, AppConfig.SystemConfig.FTP.Port);
                    _ftp = new FTPRequest(new Uri(uri), AppConfig.SystemConfig.FTP.UserName, AppConfig.SystemConfig.FTP.Password);
                }
                return _ftp;
            }
            set { _ftp = value; }
        }

        /// <summary>
        /// ftp访问路径
        /// </summary>
        public static string FtpPath
        {
            get
            {
                string localPath = AppConfig.SystemConfig.FTP.LocalPath;
                if (!Directory.Exists(localPath))
                {
                    string remotePath = @"\\" + AppConfig.SystemConfig.FTP.Server + "\\" + AppConfig.SystemConfig.FTP.ShareDir;
                    string username = AppConfig.SystemConfig.FTP.UserName;
                    string password = AppConfig.SystemConfig.FTP.Password;
                    int rst = NetFileService.Connect(remotePath, localPath, username, password, false);
                    if (rst != 0) return "";
                }
                return localPath;
            }
        }

        /// <summary>
        /// Dem入库
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="demMd"></param>
        public static void UploadDem(string fullName, DemMetaData demMd)
        {
            string guid = demMd.ID;

            string dir = System.IO.Path.GetDirectoryName(fullName);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(fullName).Trim();

            string[] files = Directory.GetFiles(dir, fileName + ".*", SearchOption.TopDirectoryOnly);

            foreach (string file in files)
            {
                string ext = System.IO.Path.GetExtension(file);
                string toName = demMd.Folder + "\\" + guid + ext;

                Ftp.UploadFile(file, toName, false);
            }


            string sql = "select * from md_cq_dem";
            DataTable metatable = RasterHelper.DbConfig.Context.GetDataTable(sql);
            
            DataRow metaDataRow = metatable.NewRow();
            metaDataRow["ID"] = guid;
            metaDataRow["名称"] = demMd.Name;
            if (demMd.Scale != null) metaDataRow["比例尺"] = demMd.Scale;
            if (demMd.Grid != null) metaDataRow["格网间距"] = demMd.Grid;
            metaDataRow["数据来源"] = demMd.DataSrc;
            metaDataRow["生产时间"] = demMd.Date;
            metaDataRow["文件目录"] = demMd.Folder;
            metaDataRow["文件格式"] = demMd.Format;

            metatable.Rows.Add(metaDataRow);
            RasterHelper.DbConfig.Context.UpdateDataTable(metatable, sql);
            metatable.AcceptChanges();
            metatable.Rows.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullName"></param>
        /// <param name="domMd"></param>
        public static void UploadDom(string fullName, DomMetaData domMd)
        {
            string guid = domMd.ID;

            string dir = System.IO.Path.GetDirectoryName(fullName);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(fullName).Trim();

            string[] files = Directory.GetFiles(dir, fileName + ".*", SearchOption.TopDirectoryOnly);

            foreach (string file in files)
            {
                string ext = System.IO.Path.GetExtension(file);
                string toName = domMd.Folder + "\\" + guid + ext;

                Ftp.UploadFile(file, toName, false);
            }

            string sql = "insert into md_cq_dom( ";
            sql += " id,名称,遥感影像类型,影像地面分辨率,色彩性质,拍摄时间,文件目录,文件格式";
            sql += " )values( ";
            sql += " :id,:name,:rsType,:resolution,:color,:psdate,:folder,:format ";
            sql += " )";
            Dictionary<string, dynamic> parameters = new Dictionary<string, dynamic>();
            parameters.Add(":id", domMd.ID);
            parameters.Add(":name", domMd.Name);
            parameters.Add(":rsType", domMd.RsType);
            parameters.Add(":resolution", domMd.Resolution);
            parameters.Add(":color", domMd.Color);
            parameters.Add(":psdate", domMd.Date);
            parameters.Add(":folder", domMd.Folder);
            parameters.Add(":format", domMd.Format);
            RasterHelper.DbConfig.Context.ExecuteNonQuery(sql, parameters);
        }
    }
}
